var str;
var counter = 0;
var question_count = 0;
var sec = 00;   
var min = 00;   
var run;

 
$(document).ready(function()
	{
		function saveValues() 
		{
			str = $("#quizer_form").serialize();
			$("#results").text(str);
		}

		$(":checkbox, :radio").click(saveValues);
		saveValues();
  });   
  
 function quizerStart(temp_questions, temp_time)
	{
		if (document.getElementById('quizer_start') != null)
			document.getElementById('quizer_start').style.display = 'none';
			
			
		if (document.getElementById('quizer_test') != null)
			document.getElementById('quizer_test').style.display = 'block';
			
		if (document.getElementById('quizer_timer') != null)
			document.getElementById('quizer_timer').style.display = 'block';
			
		if (document.getElementById('quizer_task') != null)
			document.getElementById('quizer_task').style.display = 'block';
			
		if (document.getElementById('quizer_buttons') != null)
			document.getElementById('quizer_buttons').style.display = 'block';
		
		
		if (document.getElementById('quizer_end') != null)
			document.getElementById('quizer_end').style.display = 'none';
		
		
		document.getElementById('quizer_question_id0').style.display = 'block';
		question_count = temp_questions;
		min = temp_time;
		run = 1;
		countDown();		
	} 
	
 function quizerPrev()
	{
		if (question_count < 0)
			return;
			
		if (counter) 
		{
			document.getElementById('quizer_question_id'+counter).style.display = 'none';
			counter--;
			document.getElementById('quizer_question_id'+counter).style.display = 'block';
		}
	} 

 function quizerStop()
	{
		if (question_count < 0)
			return;
			
		if (document.getElementById('quizer_test') != null)
			document.getElementById('quizer_test').style.display = 'none';
			
		if (document.getElementById('quizer_timer') != null)
			document.getElementById('quizer_timer').style.display = 'none';
			
		if (document.getElementById('quizer_task') != null)
			document.getElementById('quizer_task').style.display = 'none';
			
		if (document.getElementById('quizer_buttons') != null)
			document.getElementById('quizer_buttons').style.display = 'none';
			
		if (document.getElementById('quizer_end') != null)
			document.getElementById('quizer_end').style.display = 'block';
			
		run = 0;
		submitForm();
	} 
	
 function quizerNext()
	{
		if (question_count < 0)
			return;
			
		if (counter < question_count - 1) 
		{
			document.getElementById('quizer_question_id'+counter).style.display = 'none';
			counter++;
			document.getElementById('quizer_question_id'+counter).style.display = 'block';
		}
	}
	
 function countDown() 
	{
		sec--;
		
		if (sec == -01) 
		{
			sec = 59;
			min = min - 1;
		} 
		else
		{
			min = min;
		}
		
		if (sec <= 9)
			sec = "0" + sec;
		
		time = (min<=9 ? "0" + min : min) +" : "+ sec;
		
		if (document.getElementById("quizer_time") != null)
			document.getElementById("quizer_time").innerHTML = time; 
			
		SD = window.setTimeout("countDown();", 1000);
		
		if (min == '00' && sec == '00') 
		{ 
			sec = "00";
			window.clearTimeout(SD); 
			
			if(run = 1)				/*!!!*/
				quizerStop();
		}
	}

 function submitForm()
	{ 
		var req = null; 
		if (window.XMLHttpRequest)
		{
 			req = new XMLHttpRequest();
			if (req.overrideMimeType) 
			{
				req.overrideMimeType('text/xml');
			}
		} 
		else if (window.ActiveXObject) 
		{
			try {
				req = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e)
			{
				try {
					req = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {}
			}
	    }

		req.onreadystatechange = function()
		{ 
				if(req.status == 200)
				{
					document.getElementById("quizer_results").innerHTML  = req.responseText;
				}	
				else	
				{
					document.getElementById("quizer_results").innerHTML="Error: returned status code " + req.status + " " + req.statusText;
				}
		};
		
		req.open("POST", "quizer/quizer.php", true); 
		req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		req.send("done=true&" + str + numbers); 
	} 